#ifndef DHWL_SOCKET_TOOL_H
#define DHWL_SOCKET_TOOL_H

#include "global.h"

namespace ZY {
    class Tool
    {
        public:
            virtual ~Tool();
            static uint64_t ntohl64 ( uint64_t n);
            static uint64_t htonl64( uint64_t n ) {return Tool::ntohl64(n);}

            static string & stringAppend( string & str1 , const string & str2 ) {return str1.append( str2 );}
            static string & stringAppend( string & str1 , const char *c_str ) { return str1.append( c_str ) ;}
            static string & stringAppend( string & str1 , const char *c_str , const size_t size ) {return str1.append( c_str , size );}
            static string & stringAppend( string & str1 , const int n );
            static string & stringAppend( string & str1 , const unsigned int num );
            static string & stringAppend( string & str1 , const int64_t num );
            static string & stringAppend( string & str1 , const uint64_t num );

            static int explode( vector<string> &ret , const string &c , const string & str ) { return explode( ret , c.c_str() ,str.c_str());}
            static int explode( vector<string> &ret , const char *c , const char *str );

            static int implode( string & ret , string & c , vector<string> &str_vec );
            static int implode( string & ret , char *c , vector<char*> &str_vec );

            static double microtime();

            static int64_t getRand( int64_t min , int64_t max );
            static int getRand( int min , int max );

            static time_t getLocalTime( time_t t = 0 );
            static string dateTime();

            static void toUpper( string & s );

            static void toLower( string & s );

            static map< string , string > getHttpRespondHeaderMap( string headerStr );

            //下标为返回值，值为机率
            static int getRandFromLst( int perMap[] , int num );

            static string getDateTime();

            typedef unsigned char BYTE;

            inline static BYTE   toHex(const BYTE &x);

            inline static string URLEncode(const string &sIn);

            static string getCalltrac();
            static size_t getCalltrac( char *buff );
            /**
               检查唯一性运行，如果发现有运行中的进程或其它错误，则返回false
            */


            static char* getFileName(char *arg0);
            static bool  checkOnlyRun( const char *filename , const char *pidFile );
            static bool checkOnlyRun( string filename , string pid_file ){ return checkOnlyRun( filename.c_str() , pid_file.c_str() );}

           inline static  string   intToString( int num ) {   ostringstream   stream;     stream  <<  num;     return   stream.str(); }
           inline static int   stringToInt( string  str ) { int num; istringstream   stream( str ); stream  >> num;    return   num;  }

           static void CompressSelf( string &str );
           static void Compress( string &des_str , string &src_str );
           static string Compress( string &str );

            static void unCompressSelf( string &str );
           static void unCompress( string &des_str , const string &src_str );
           static string unCompress( const string &str );

            static string toString( const int8_t );
           static string toString( const short );
           static string toString( const int );
           static string toString( const int64_t );
           static string toString( const double );
           static string toString( const char *);
           static string toString( const char* , size_t );
           static string toString( const string &);

        protected:
        private:
            Tool();

    };


    Tool::BYTE Tool::toHex(const Tool::BYTE &x)
    {
        return x > 9 ? x + 55: x + 48;
    }

    string Tool::URLEncode(const string &sIn)
    {
        // cout << "size: " << sIn.size() << endl;
        string sOut;
        for( size_t ix = 0; ix < sIn.size(); ix++ ) {
            BYTE buf[4];
            memset( buf, 0, 4 );
            if( isalnum( (BYTE)sIn[ix] ) ) {
                buf[0] = sIn[ix];
            } else if ( isspace( (BYTE)sIn[ix] ) ) {
                buf[0] = '+';
            } else {
                buf[0] = '%';
                buf[1] = toHex( (BYTE)sIn[ix] >> 4 );
                buf[2] = toHex( (BYTE)sIn[ix] % 16);
            }
            sOut += (char *)buf;
        }
        return sOut;
    };

}
#endif // TOOL_H
